// components/good-item-info/good-item-info.js
let app = getApp();
Component({

  options: {
    addGlobalClass: true,//使用app.wxss样式
    styleIsolation: 'shared'
  },

  /**
   * 组件的属性列表
   */
  properties: {
    goods: {
      type: Object,
      //监听数据变化
      observer(newVal,oldVal,changedPath){
        if(Object.keys(newVal).length != 0) {//数据不为空
          this.setData({goodsData: newVal})
          this.gogo();
        }
      }
    },
    commentInfo:{
      type: Object,
      //监听数据变化
      observer(newVal,oldVal,changedPath){
        if(Object.keys(newVal).length != 0) {//数据不为空
          this.setData({commentInfo: newVal})
        }
      }
    },
    serverTime: Number,//当前时间
  },

  /**
   * 组件的初始数据
   */
  data: {
    swiperList: [],
    goodsData: {},
    priceTime: '',//倒计时
    timeData: {},
    statusText: '',//商品状态文案
    picArr: [],//标签

    isShowShare: false,//控制分享组件
    thenGoodsId: '',//分享组件需要使用goodsId查询数据
    playing: false,//视频是否在播放

    commentInfo: {},
  },

  lifetimes: {
    attached(){
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {

    //参加拼团
    joinAssemble(event){
      let obj = {
        goods: this.data.goodsData,
        mid: event.currentTarget.dataset.mid,
        orderid: event.currentTarget.dataset.orderid
      }
      this.triggerEvent('assembleSuffer', obj);
    },

    //点击分享
    shareClick (event) {
      let _this = this;
      let sd = {
        id: _this.data.goodsData.id,
        thumb: _this.data.goodsData.thumb,
        title: _this.data.goodsData.title
      };
      this.triggerEvent('shareGoodsIdSuffer', sd);//将分享需要数据传给页面使用
      this.setData({
        isShowShare: true,
        thenGoodsId: _this.data.goodsData.id,
      })
    },
    //接收分享
    isShowShareSuffer (event) {
      this.setData({
        isShowShare: event.detail,
      })
    },

    //有数据后执行
    gogo(){
      let _this = this;
      this.setSwiper();

      //处理拼团头像和名字
      if(_this.data.goodsData.spellGroupOrdS){
        _this.data.goodsData.spellGroupOrdS.filter((item,index)=>{
          _this.setData({
            ['goodsData.spellGroupOrdS['+ index + '].niknames']: item.niknames.slice(0, -1).replace(',','、')
          })
        })
      }

      this.setData({
        statusText: _this.judgeStatusText(),
        priceTime: _this.judgePriceTime(),
        picArr: _this.data.goodsData.prodlabels.split(","),
      })
    },

    //判断倒计时数据
    judgePriceTime(){
      if (this.startTime() > 0) {
        return this.startTime();
      } else if (this.endTime() > 0) {
        return this.endTime();
      } else {
        return 0;
      }
    },

    //判断商品状态文案
    judgeStatusText(){
      if (this.endTime() === 0) {
        return '团购已结束';
      }

      if (this.startTime() > 0) {
        return '即将开团';
      }

      if (this.endTime() > 0) {
        if (this.isSoldOut()) {
          return '已售罄';
        }
        if(this.data.goodsData.isSpellgroup == 1){
          return '距离拼团结束还剩';
        }else if(this.data.goodsData.ismiaosha == 1){
          return '距离秒杀结束还剩';
        }else{
          return '距离团购结束还剩';
        }
      }
    },

    //是否售罄 true卖完了
    isSoldOut() {
      return this.data.goodsData.total <= (this.data.goodsData.salesreal);
    },
    endTime() {
      let time = Date.parse(this.data.goodsData.timeend.replace(/-/g, '/')) - this.properties.serverTime;
      return (time > 0) ? time : 0;
    },
    startTime() {
      let time = Date.parse(this.data.goodsData.timestart.replace(/-/g, '/')) - this.properties.serverTime;
      return (time > 0) ? time : 0;
    },

    //监听倒计时
    timeChange(e) {
      this.setData({
        timeData: e.detail
      });
    },

    //得到轮播图
    setSwiper() {
      let _this = this;
      let images = [_this.data.goodsData.thumb.slice(0, 4) === "http" ? _this.data.goodsData.thumb : app.globalData.IMAGE_PREFIX + _this.data.goodsData.thumb];
      let thumbUrl = _this.data.goodsData.thumbUrl;

      if (!thumbUrl) {
        _this.setData({
          swiperList: images
        })
        return;
      }


      if (thumbUrl.slice(0, 1) === 'a') {
        let tmp = thumbUrl.slice(4);
        let content = tmp.split(';');

        content.forEach(element => {
          if (element.slice(0, 1) === "s") {
            let urls = element.split(':');
            let url = urls[2];

            for (let i = 3; i < urls.length; ++i) {
              url += ":" + urls[i];
            }

            url = url.substr(1, url.length - 2);//去除两端引号
            url = (url.slice(0, 7) === "http://") ? url : IMAGE_PREFIX + url;
            images.push(url);
          }
        });
      } else {
        let url = thumbUrl.split(',');
        images.push(...url);
      }

      _this.setData({
        swiperList: images
      })
    },

    //播放视频
    playVideo(){
      this.setData({ playing: true })
    },
    //播放结束
    endVideo(){
      this.setData({ playing: false })
    },
    //播放错误
    errorVideo(){
      wx.showToast({
        title: '视频播放错误',
        inco: 'none'
      })
      this.setData({ playing: true })
    },

  }
})
